package top.vanchor.flexible.config.jwtconfig;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2021/11/10 13:26
 */

import top.vanchor.flexible.utils.JwtInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class webConfig implements WebMvcConfigurer {

    /**
     * 配置全局跨域请求
     * @param registry 注册
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*");
    }

    /**
     * 设置全局拦截和放行---【全局规则（一级规则）】
     * @param registry 注册
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new JwtInterceptor());
        // 需拦截的路径------------------------------------------------------------------------
        interceptorRegistration.addPathPatterns("/**");
        // 需放行的文件路径------------------------------------------------------------------------
        interceptorRegistration.excludePathPatterns("/**.jpg", "/**.jpeg", "/**.png", "/**.mp4","/favicon.ico","/error");
        // 需放行的接口路径
        interceptorRegistration.excludePathPatterns(
                // 测试BUG时可以放行所有接口
//                "/**",
                // 认证失败重定向接口
                "/noAuthorization",
                //下载文件接口
                "/public/downLoadPdf",
                "/public/jump",
                "/public/getVertifyCode",
                "/public/verify",
                "/public/getPdf",
                "/public/executePdfOfHMC",
                // 发送短信请求接口
                "/**/sendShortSMS",
                // 注册接口
                "/**/register",
                // 后台管理员登录接口
                "/admin/adminUser/login",
                // 后台管理员登出接口
                "/admin/adminUser/logout",
                // 用户登录接口
                "/user/login",
                // 用户获取短信验证码
                "/user/getSmsCode",
                //支付回调接口
                "/user/alipayReturn",
                //获取救助列表
                "/user/getRescueList",
                //文件导出
                "/**/doExport",
                "/**/exportTemplate",
                "/**/exportTemplateQuery",
                "/**/exportTemplateDismiss",
                "/**/exportError",
                "/**/exportQuery",
                "/**/downloadPositionNoGood",
                "/**/test",
                "/api/**",
                //swagger
                "/swagger-ui.html/**",
                "/swagger-resources/**",
                "/v2/**",
                "/doc.html/**",
                "/webjars/**",
                "/csrf",
                "/",
                //user
                "/user/**"
        );
    }
}
